<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="util.*"%>
<%@page import="dal.*"%>
<%@ page import="model.*,java.util.*"%>
<%
int idChecklist = 0;
if (request.getParameter("idChecklist") != null
		&& !request.getParameter("idChecklist").isEmpty()) {
	try {
		idChecklist = Integer.parseInt(request
				.getParameter("idChecklist"));
	} catch (Exception e) {
	}
}
%>

<%
	GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(
			new ChecklistItemModel());
	ChecklistItemModelExt queryCount = new ChecklistItemModelExt();
	
	if(idChecklist != 0) // Khai added: Filter checklistItem(s) of just current Checklist
		queryCount.setIdChecklist(idChecklist);
	
	int count = dal.count(queryCount);

	int pageIndex = 1;
	if (request.getParameter("pageIndex") != null
			&& !request.getParameter("pageIndex").isEmpty()) {
		try {
			pageIndex = Integer.parseInt(request
					.getParameter("pageIndex"));
		} catch (Exception e) {
		}
	}

	int pageSize = 20;
	if (request.getParameter("pageSize") != null
			&& !request.getParameter("pageSize").isEmpty()) {
		try {
			pageSize = Integer.parseInt(request
					.getParameter("pageSize"));
		} catch (Exception e) {
		}
	}

	int noOfPage = count / pageSize;
	if (count % pageSize > 0) {
		noOfPage++;
	}

	ChecklistItemModelExt query = new ChecklistItemModelExt();
	if(idChecklist != 0)// Khai add: Filter checklistItem(s) of just current Checklist
		query.setIdChecklist(idChecklist);
	query.setLimit(pageSize);
	query.setOffset((pageIndex - 1) * pageSize);
	List<ChecklistItemModel> list = dal.selectList(query);

	boolean embedded = false;
	if (request.getParameter("embedded") != null
			&& !request.getParameter("embedded").isEmpty()) {

		if (request.getParameter("embedded").equalsIgnoreCase("true")
				|| request.getParameter("embedded").equalsIgnoreCase(
						"1")) {
			embedded = true;
		}
	}
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Checklist Item List</title>

<link rel="stylesheet" href="css/common.css" />
<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
<link rel="stylesheet" href="css/jquery-ui-timepicker.css" type="text/css" />
<link rel="stylesheet" href="css/hyjack.css" type="text/css" />
<link rel="stylesheet" href="css/datepicker.css" />

<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/common.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery-1.6.1.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.select.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.core.min.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.ui.widget.min.js"></script>

<script language="javascript" type="text/javascript" src="js/jquery.ui.timepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
<script language="javascript" type="text/javascript" src="js/jquery.hyjack.js"></script>
<script language="javascript" type="text/javascript" src="js/ajaxQuickInsert.js"></script>

<link rel="stylesheet" href="css/listForm.css" />

</head>
<body>

	<%
		if (!embedded)
		{
	%>
	<jsp:include page="menu.jsp"></jsp:include>
    <br />
	<%
		}
	%>
	<b>Number of Checklist Items:<%=count%></b>
	<br />
	<br/>
	<%
		if(noOfPage>1)
		{
	%>
	<table class="tblPaging">
		<tr class="trPaging">
			<td class="tdPaging">				
                <%
					for (int i = 1; i <= noOfPage; i++) {
						if (i == pageIndex) {
				%> 
					<span class="lblPaging"><%=i%></span> 
				<%
				 		} else {
				 %> 
				 	<a class="hrefPaging" href="checklistItemList.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>"><%=i%></a>
				<%
						}
					}
				%>
			</td>
		</tr>
	</table>
	<%} %>
    <br />
	<table class="tblList">
		<tr class="tblListHeader">			
			<th class="tdListHeader"></th>
            <th class="tdListHeader"></th>
			<th class="thListHeader">Id</th>
			<th class="thListHeader" width="50px">Checklist</th>
			<th class="thListHeader" width="50px">Display Index</th>
			<th class="thListHeader" width="120px">Name</th>
			<th class="thListHeader" width="250px">Remark</th>
			<th class="thListHeader" width="30px">Is Group</th>
			<th class="thListHeader" width="50px">Employee</th>
			<th class="thListHeader" width="50px">Company</th>
			<th class="thListHeader">Start Time</th>
			<th class="thListHeader">End Time</th>
			<th class="thListHeader">Status</th>
			<th class="thListHeader">Parent</th>
            <th class="tdListHeader"></th>
		</tr>

		<%
			int size = list.size();
			for (int i = 0; i < size; i++) {
				ChecklistItemModel obj = list.get(i);
				int idEmployee=obj.getIdEmployee();
				int idCompany=obj.getIdCompany();
				int idParent=obj.getIdParent();
				int status=obj.getStatus();
		%>

		<tr class="trListItem">
			<td class="tdListInt"><%=i + 1%></td>
			<td class="tdListHref Edit"><a href="checklistItem?mode=edit&id=<%=obj.getId()%>">Edit</a></td>
			<td class="tdListInt"><%=obj.getId()%></td>
			<td class="tdListInt"><%=CacheUtil.getChecklistName(obj.getIdChecklist())%></td>
			<td class="tdListText"><%=obj.getDisplayIndexToString()%></td>
			<td class="tdListText"><%=obj.getNameToString()%></td>
			<td class="tdListTextarea"><%=obj.getRemarkToString()%></td>
			<td class="tdListBool"><%=HtmlUtil.genHtml(obj.getIsGroup())%></td>
			<td class="tdListInt"><%=CacheUtil.getEmployeeName(idEmployee)%></td>
			<td class="tdListInt"><%=CacheUtil.getCompanyName(idCompany)%></td>
			<td class="tdListDateTime"><%=StringUtil.toDateTimeString(obj.getStartTime())%></td>
			<td class="tdListDateTime"><%=StringUtil.toDateTimeString(obj.getEndTime())%></td>
			<td class="tdListInt"><%=StringUtil.getChecklistItemStatus(status)%></td>
			<td class="tdListInt"><%=CacheUtil.getChecklistItemName(idParent)%></td>
            <td class="tdListHref">
				<a href="checklistItem?mode=delete&id=<%=obj.getId()%>&idChecklist=<%=idChecklist%>" onclick="return confirm('Do you really want to delete this checklist item ?')">Delete</a>
			</td>
		</tr>
		<%
			}
		%>
	</table>
    <br />
    <%
    	if(noOfPage>1)
    	{
    %>
    <table class="tblPaging">
		<tr class="trPaging">
			<td class="tdPaging">				
                <%
					for (int i = 1; i <= noOfPage; i++) {
						if (i == pageIndex) {
				%> 
					<span class="lblPaging"><%=i%></span> 
				<%
				 		} else {
				 %> 
				 	<a class="hrefPaging" href="checklistItemList.jsp?pageIndex=<%=i%>&pageSize=<%=pageSize%>"><%=i%></a>
				<%
						}
					}
				%>
			</td>
		</tr>
	</table>
	<%} %>
</body>
</html>